package com.boot.unity.api.wx.controller;

import com.alibaba.fastjson.JSONObject;

import com.boot.unity.api.utils.EndeCrypt;
import com.boot.unity.api.wx.service.CommonUtilUrlService;
import com.boot.unity.api.wx.service.WxUserService;
import com.boot.unity.api.wx.utils.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户服务
 */
@RestController
@RequestMapping("/wx/user")
@Validated
public class WxUserController {
    private final Log logger = LogFactory.getLog(WxUserController.class);

    @Autowired
    private WxUserService wxUserService;

    @Value("${serv.servurl}")
    private String servurl;

    @Autowired
    private CommonUtilUrlService commonUtilUrlService;


    @GetMapping("queryUserByWxid")
    public Object queryUserByWxid(String id,String key) {
       return wxUserService.queryUserByWxid(id,key);
    }

    @GetMapping("bdingPcUser")
    public Object bdingPcUser(String id) {
        return wxUserService.bdingPcUser(id);
    }

    @GetMapping("bindUser")
    public Object bindUser(String id, String name, String mobile, String cardNo, String code) throws Exception {
        // String code = stu.getCode();
        mobile = EndeCrypt.deCrypt(mobile);
        JSONObject paramJson = new JSONObject();
        paramJson.put("phoneNumber",mobile);
        paramJson.put("code",code);
        String msgUrl = servurl+"/wx/bindManager/queryMoreNowCode";
        String result = commonUtilUrlService.post(msgUrl, paramJson.toString());
        Thread.sleep(500);
        if(StringUtils.isEmpty(result)){
            return ResponseUtil.fail(404, "验证码错误-1");
        }
        if(!result.equals(code)){
            return ResponseUtil.fail(404, "验证码错误-2");
        }
        cardNo = EndeCrypt.deCrypt(cardNo);

        // System.out.println(code);
        return wxUserService.bindUser(id,name,mobile,cardNo,code);
    }

    /**
     * 获取签署状态
     * @param userid 用户id
     * @return 签署状态
     */
    // @PostMapping("getsigningStatus")
    @GetMapping("getsigningStatus")
    public Object getsigningStatus(String userid) {
        return wxUserService.getsigningStatus(userid);
    }

    /**
     * 获取userid对应的step
     * */
    @GetMapping("getuserStep")
    public Object getUserStep(String id) {
        return wxUserService.getUserStep(id);
    }



    /*@PostMapping("againHtUrl")
    @GetMapping("againHtUrl")
    public Object againHtUrl(String userid) {
        return wxUserService.againHtUrl(userid);

    }*/

    @GetMapping("getUserDetail")
    public Object getUserDetail(String id,String key) {
        return wxUserService.getUserDetail(id,key);

    }

    @GetMapping("payOkList")
    public Object payOkList(String id,String key) {
        return wxUserService.payOkList(id,key);
    }
    @GetMapping("getStuDetailForId")
    public Object getStuDetailForId(Integer id,String key) {
        return wxUserService.getStuDetailForId(id,key);
    }


    /**
     * 根据userid获取E签宝认证链接
     * @param userid 用户
     * @return 签署链接
     */
    @GetMapping("queryAuthUrlByUserid")
    public Object queryAuthUrlByUserid(String userid) {
        return wxUserService.queryAuthUrlByUserid(userid);
    }

}
